﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolutionModel;
using SFast.DB;
//using EntityFramework.Extensions;
using System.Linq.Expressions;
using System.Data.Objects.DataClasses;

namespace SolutionDAL
{
    public class DBBaseDAL<T> where T : class
    {
        #region 
        //#region 单一表统一添加数据
        ///// <summary>
        ///// 单一表统一添加数据
        ///// </summary>
        ///// <param name="entity"></param>
        ///// <returns></returns>
        //public virtual int Add(T entity)
        //{
        //    using (PurviewDBEntities db = new PurviewDBEntities())
        //    {
        //        try
        //        {
        //            db.AddObject(typeof(T).Name, entity);
        //            return db.SaveChanges();
        //        }
        //        catch (Exception ex)
        //        {
        //            throw ex;
        //        }
        //    }
        //}
        //#endregion

        //#region 单一表统一更新数据
        ///// <summary>
        ///// 单一表统一更新数据函数
        ///// </summary>
        ///// <param name="filterExpression">更新条件</param>
        ///// <param name="updateEntity">更新对象（新数据）</param>
        ///// <returns></returns>
        //public virtual int Update(Expression<Func<T, bool>> filterExpression, T updateEntity)
        //{
        //    using (PurviewDBEntities db = new PurviewDBEntities())
        //    {
        //        var newExpression = Expression.New(typeof(T));
        //        var memberInit = Expression.MemberInit(newExpression,
        //            typeof(T).GetProperties()
        //            .Where(p => p.CanRead && p.CanWrite)
        //            .Where(p => p.DeclaringType != typeof(EntityObject))
        //            .Where(p => !(p.GetCustomAttributes(true).OfType<EdmScalarPropertyAttribute>().FirstOrDefault() != null
        //                && p.GetCustomAttributes(true).OfType<EdmScalarPropertyAttribute>().FirstOrDefault().EntityKeyProperty))
        //            .Select(p => Expression.Bind(p, Expression.Constant(p.GetValue(updateEntity, null))))
        //            .ToArray());

        //        var lambda = Expression.Lambda<Func<T, T>>(memberInit, Expression.Parameter(typeof(T), "updateEntity"));

        //        var entity = db.CreateObjectSet<T>();
        //        return entity.Update(filterExpression, lambda);
        //    }

        //}
        //#endregion

        //#region 单一表统一删除数据
        ///// <summary>
        ///// 单一表统一删除数据
        ///// </summary>
        ///// <param name="filterExpression">删除表达试</param>
        ///// <returns></returns>
        //public virtual int Delete(Expression<Func<T, bool>> filterExpression)
        //{
        //    using (PurviewDBEntities db = new PurviewDBEntities())
        //    {
        //        var entity = db.CreateObjectSet<T>();
        //        return entity.Delete(filterExpression);
        //    }
        //}
        //#endregion

        //#region 单一表统一获取对象
        ///// <summary>
        ///// 单一表统一获取对象 
        ///// </summary>
        ///// <param name="predicate">条件表达式</param>
        ///// <returns></returns>
        //public virtual T GetObject(Expression<Func<T, bool>> predicate)
        //{
        //    using (PurviewDBEntities db = new PurviewDBEntities())
        //    {
        //        var entity = db.CreateObjectSet<T>();
        //        return entity.FirstOrDefault(predicate);
        //    }
        //}
        //#endregion

        #endregion

        /// <summary>
        /// 获取表数据
        /// </summary>
        /// <param name="pageSize">每页显示大小</param>
        /// <returns></returns>
        //public virtual Pagination<T> GetList(int pageSize)
        //{
        //    using (PurviewDBEntities db = new PurviewDBEntities())
        //    {
        //        var entity = db.CreateObjectSet<T>();
        //        return entity._ToPagination(pageSize);
        //    }
        //}

        /// <summary>
        /// 获取表数据
        /// </summary>
        /// <param name="pageSize">每页显示大小</param>
        /// <param name="predicate">条件表达示</param>
        /// <returns></returns>
        //public virtual Pagination<T> GetList(int pageSize,Expression<Func<T, bool>> predicate)
        //{
        //    using (PurviewDBEntities db = new PurviewDBEntities())
        //    {
        //        var entity = db.CreateObjectSet<T>();
        //        return entity.Where(predicate)._ToPagination(pageSize);
        //    }
        //}

    }
}
